package dao

import (
	"github.com/jinzhu/gorm"
	"go-mall/app/usercenter-svc/internal/model"
	"go-mall/libs/database"
	"log"
)

type Dao struct {
	db *gorm.DB
}

func New() *Dao {
	return &Dao{
		db: database.Mysql(),
	}
}

func (d *Dao) GetUserByUserNameAndPassword(name string, password string) *model.MallUsers {
	user := &model.MallUsers{}
	if err := d.db.Table("mall_users").Where("user_name = ? and password = ? ", name, password).First(user).Error; err != nil {
		log.Println(err)
		return nil
	}
	return user
}

func (d *Dao) GetUserByUserName(name string) *model.MallUsers {
	user := &model.MallUsers{}
	if err := d.db.Table("mall_users").Where("user_name = ?", name).First(user).Error; err != nil {
		log.Println(err)
		return nil
	}
	return user
}

func (d *Dao) CreateUser(user *model.MallUsers) error {
	if err := d.db.Table("mall_users").Create(user).Error; err != nil {
		log.Printf("CreateWqUserInvite error %v ", err)
		return err
	}
	return nil
}
